package com.muyu.danmo.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoMountLikes;
import com.muyu.danmo.service.IDanmoMountLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 装裱点赞Controller
 *
 * @author yangle
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/mountLikes")
public class    DanmoMountLikesController extends BaseController {
    @Autowired
    private IDanmoMountLikesService danmoMountLikesService;

    /**
     * 查询装裱点赞列表
     */
//    @RequiresPermissions("system:likes:list")
    @GetMapping("/list")
    public Result<TableDataInfo<DanmoMountLikes>> list(DanmoMountLikes danmoMountLikes) {
        startPage();
        List<DanmoMountLikes> list = danmoMountLikesService.selectDanmoMountLikesList(danmoMountLikes);
        return getDataTable(list);
    }

    /**
     * 导出装裱点赞列表
     */
//    @RequiresPermissions("system:likes:export")
    @Log(title = "装裱点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoMountLikes danmoMountLikes) {
        List<DanmoMountLikes> list = danmoMountLikesService.selectDanmoMountLikesList(danmoMountLikes);
        ExcelUtil<DanmoMountLikes> util = new ExcelUtil<DanmoMountLikes>(DanmoMountLikes.class);
        util.exportExcel(response, list, "装裱点赞数据");
    }

    /**
     * 获取装裱点赞详细信息
     */
//    @RequiresPermissions("system:likes:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        System.out.println(id);
        return success(danmoMountLikesService.selectDanmoMountLikesById(id));
    }

    /**
     * 新增装裱点赞
     */
//    @RequiresPermissions("system:likes:add")
    @Log(title = "装裱点赞", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoMountLikes danmoMountLikes) {
        return Result.success(danmoMountLikesService.insertDanmoMountLikes(danmoMountLikes));
    }

    /**
     * 修改装裱点赞
     */
//    @RequiresPermissions("system:likes:edit")
    @Log(title = "装裱点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoMountLikes danmoMountLikes) {
        return toAjax(danmoMountLikesService.updateDanmoMountLikes(danmoMountLikes));
    }

    /**
     * 删除装裱点赞
     */
//    @RequiresPermissions("system:likes:remove")
    @Log(title = "装裱点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(danmoMountLikesService.deleteDanmoMountLikesByIds(ids));
    }


    /**
     * 装裱点赞
     * @param id
     * @return
     */
    @GetMapping("/like/{id}")
    public Result likeMountLikes(@PathVariable Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUserid() == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
//        if (userId.equals(mountMessageId)) {
//            return Result.error("不能点赞自己");
//        }
        return danmoMountLikesService.likeMountLikes(userId, id);
    }
}
